package framework.common;

public class Client {

	private String user;
	private HubServer hub;
	private String deviceName;

	public Client(String user) {
		this.user = user;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public HubServer getHub() {
		return hub;
	}

	public void setHub(HubServer hub) {
		this.hub = hub;
	}

	public String getDeviceName() {
		return this.deviceName;
	}

	public void setDeviceName(String deviceName) {
		this.deviceName = deviceName;
	}

	public void connect(DeviceServer dev) {
		this.setDeviceName(dev.getDeviceName());
	}

	// 便利なメソッド
	private DeviceRequest tmpRequest, request;

	public void begin() {
		tmpRequest = new DeviceRequest(this);
	}

	public void addRequest(SensorRequest req) {
		tmpRequest.add(req);
	}

	public void addRequest(int command, String attr, Object value) {
		tmpRequest.add(new SensorRequest(command, attr, value));
	}

	public void addRequest(int command, String attr, int value) {
		addRequest(command, attr, Integer.valueOf(value));
	}

	public void addRequest(int command, String attr, double value) {
		addRequest(command, attr, Double.valueOf(value));
	}

	public void end() {
		request = tmpRequest;
		tmpRequest = null;
	}

	/**
	 * begin-endで作成された要求を返す。
	 *
	 * @return 作成された要求
	 */
	public DeviceRequest getRequest() {
		return request;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((deviceName == null) ? 0 : deviceName.hashCode());
		result = prime * result + ((hub == null) ? 0 : hub.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Client))
			return false;
		Client other = (Client) obj;
		if (deviceName == null) {
			if (other.deviceName != null)
				return false;
		} else if (!deviceName.equals(other.deviceName))
			return false;
		if (hub == null) {
			if (other.hub != null)
				return false;
		} else if (!hub.equals(other.hub))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}

}
